<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('Entity/Doc_Menus_Entity.php');
class Doc_Menus_Model extends Doc_Menus_Entity
{
    public function __construct()
    {
        parent::__construct();
    }


    public function getIdByAlias($alias)
    {
        $sql = "SELECT id FROM doc_menus WHERE alias = ?";
        $query = $this->db->query($sql, array($alias));
        $obj = $query->row();
        if ($obj) {
            return $obj->id;
        } else {
            return 0;
        }
    }
    


    public function dataSort($field, $order)
    {
        $xOrder = $order == 'asc' ? 'desc' : 'asc';
        $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';
        $url = anchor('doc_menus/sort/' . $field . '/' . $xOrder, t($field) . $img);
        $_SESSION['sm_sort_doc_menus'] = array(
            'field' => $field,
            'order' => $order,
            'url' => $url);
    }

    public function dataIndex($target)
    {
        $numshow = 25;
        $this->db->select('id, title, created, status, order');
        $this->db->from('doc_menus');
        if ($this->session->userdata('sm_find_doc_menus')) {
            $this->db->like('doc_menus.title', $this->session->userdata('sm_find_doc_menus'));
        }
        if (isset($_SESSION['sm_sort_doc_menus'])) {
            $sort = $_SESSION['sm_sort_doc_menus'];
            $this->db->order_by('doc_menus.' . $sort['field'], $sort['order']);
        } else {
            $this->db->order_by('doc_menus.created', 'desc');
        }
        $this->db->limit($numshow, $target);
        $query = $this->db->get();
        $paging = paging_admin('doc_menus/index', $this->_total(), $numshow, 4);
        return array(
            'query' => $query,
            'paging' => $paging,
            'formFind' => $this->formFind());
    }

    private function _total()
    {
        return $this->db->count_all_results("doc_menus");
    }

    private function _checkdoc_menusById($id)
    {
        $query = $this->db->get_where("doc_menus", array("id" => $id));
        if ($query->num_rows() > 0)
            return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")), "doc_menus");
            return false;
        }
    }

    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('doc_menus/find'), $attributes);
        $data = array(
            'name' => 'txt_find',
            'id' => 'txt_find',
            'value' => '',
            'style' => 'width:200px');
        $f .= form_input($data);
        $s = array(
            'name' => 'submit',
            'value' => t('Find'),
            'class' => 'ibutton green',
            'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_doc_menus')) {
            $r = array(
                'name' => 'btnReset',
                'value' => 'Reset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }


    public function dataEdit($id)
    {
        if (!$this->_checkdoc_menusById($id))
            return redirect(site_url('doc_menus'));
        $query = $this->db->get_where('doc_menus', array('id' => $id));
        return array('query' => $query);
    }

    public function dataAdd()
    {
    }

    public function checkForm()
    {
        $this->form_validation->set_rules('title', t('title'), 'trim|required');
        $this->form_validation->set_rules('created', t('created'), 'trim|required');
        $this->form_validation->set_rules('status', t('status'), 'trim');
        $this->form_validation->set_rules('order', t('order'), 'trim|required|integer');
        return $this->form_validation->run();
    }

    public function doUpdate()
    {
        $this->load->helper('text');
        $title = $this->input->post('title');
        $time = strtotime($this->input->post('created'));
        
        $this->setTitle($title);
        //$this->setAlias(iurl_title($title));
        $alias = isset($_POST['alias']) ? $_POST['alias'] : iurl_title($title);
        $this->setAlias($alias);
        $this->setCreated($time);
        $this->setStatus($this->input->post('status'));
        $this->setOrder($this->input->post('order'));
        
        
        
        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        } else {
            $this->setCreated(time());
        }
        return $this->save();
    }
    

    public function menuDel($id)
    {
        $sql = "SELECT id FROM doc_categories WHERE mid = ?"; 
        $query = $this->db->query($sql, array($id));
        $obj = $query->row();
        if ($obj) {
            return false;
        }
        $this->del($id);
        return true;
    }
}
